package com.fcscs.cservice.client.main.reservation;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.core.gwt.common.ui.SimpleGridPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.reservation.BillingModelData;
import com.fcscs.cservice.client.common.model.reservation.GenderGWTEnum;
import com.fcscs.cservice.common.CServiceReportConstants;
import com.fcscs.cservice.domain.model.BillingView;
import com.fcscs.cservice.domain.model.FolioView;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.cservice.domain.model.ReservationConfView.PaymentMethod;
import com.fcscs.cservice.domain.model.ReservationConfView.Title;
import com.fcscs.share.gwt.common.HttpHelper;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Frame;

public class FolioDialog extends BaseFormDialog<FolioView> {
	private static FolioDialog INSTANCE;
	private MyFormPanel formPanel;
	private boolean isFirst;
	private ReservationConfView reservationConf;
	private Frame reportFrame; 
	protected FormButton exportBtn;
	private FolioView currentFolio;
	
	public FcsFormPanel<FolioView> getFormPanel() {
		return formPanel;
	}

	public static FolioDialog getINSTANCE(ReservationConfView reservationConf){
		if(null == INSTANCE){
			INSTANCE = new FolioDialog();
		}
		INSTANCE.reservationConf = reservationConf;
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.folio());
		return INSTANCE;
	}
	
	private FolioDialog(){
		formPanel = new MyFormPanel(null);
		formPanel.setId("FolioDialog_formPanel");
		isFirst = true;
		formPanel.setLabelWidth(120);
		add(formPanel);
		setScrollMode(Scroll.AUTOY);
		setSize(1000, 700);
	}
	
	private class MySimpeGridPanel extends
	    SimpleGridPanel<BillingModelData>{

		public MySimpeGridPanel(
				String heading,
				ModelSelectListener<BillingModelData> modelSelectListener) {
			super(heading, null, (Object[]) null);
			setCollapsible(false);
			setSize(565, 160);
		}
		
		@Override
		protected ColumnModel createColumnConfig(Object... params) {
			List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			
			ColumnConfig columnConfig = CoreUtil.createNonSortableColumnConf(ShareGWTConstants.DATE,
					CoreConstants.CORE_CONSTANTS.date(), 100);
			columns.add(columnConfig);

			columns.add(CoreUtil.createNonSortableColumnConf(ShareGWTConstants.TIME,
					CoreConstants.CORE_CONSTANTS.time(), 100));
			columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.REFERENCE,
					CServiceConstants.CSERVICE_CONSTANTS.reference(), 100));
			columns.add(CoreUtil.createNonSortableColumnConf(CoreConstants.DESCRIPTION,
					CoreConstants.CORE_CONSTANTS.description(), 100));
			columns.add(CoreUtil.createNonSortableColumnConf(ShareGWTConstants.AMOUNT,
					CoreConstants.CORE_CONSTANTS.amount(), 100));
			
			ColumnModel columnModel = new ColumnModel(columns);
			return columnModel;
		}
	}
	
	private class MyFormPanel extends FcsFormPanel<FolioView>{
		private LayoutContainer lc1;
		
		private Label folioNoLabel;
		private AdapterField folioNoAF;
		
		private Label guestNameLabel;
		private AdapterField guestNameAF;
		
		private Label titleLabel;
		private AdapterField titleAF;
		
		private Label genderLabel;
		private AdapterField genderAF;
		
		private Label noOfGuestLabel;
		private AdapterField noOfGuestAF;
		
		private Label vipCodeLabel;
		private AdapterField vipCodeAF;
		
		private Label roomNoLabel;
		private AdapterField roomNoAF;
		
		private Label arrivalDateAndTimeLabel;
		private AdapterField arrivalDateAndTimeAF;
		
		private Label departureDateAndTimeLabel;
		private AdapterField departureDateAndTimeAF;
		
		private Label payMethodLabel;
		private AdapterField payMethodAF;
//		private EasyClearCombobox<PaymentMethodModelData> paymentMethodCombo;
//		private ListStore<PaymentMethodModelData> paymentMethodListStore;
		
		private Label remarksLabel;
		private AdapterField remarksAF;
		
		private MySimpeGridPanel simGridPanel;
		
		private Label totalAmountLabel;
		private AdapterField totalAmountAF;
		
		private FormButton okButton;

		private LayoutContainer lc2;
		
		protected MyFormPanel(FormSubmitListener<FolioView> l) {
			super(l);
			setHeaderVisible(false);
			
			
			
			lc1 = new LayoutContainer();
			lc1.setLayout(new ColumnLayout());
			lc1.setBorders(true);
			
			LayoutContainer left1 = new LayoutContainer();
			left1.setId("FolioDialog_left1");
			
			FormLayout layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(160);
			layout.setDefaultWidth(160);
			left1.setLayout(layout);
			
			folioNoLabel = new Label();
			folioNoLabel.setId("FolioDialog_folioNoLabel");
			
			folioNoAF = new AdapterField(folioNoLabel);
			folioNoAF.setId("FolioDialog_folioNoAF");
			folioNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.folioNo());
			left1.add(folioNoAF);
			
			guestNameLabel = new Label();
			guestNameLabel.setId("FolioDialog_guestNameLabel");
			
			guestNameAF = new AdapterField(guestNameLabel);
			guestNameAF.setId("FolioDialog_guestNameAF");
			guestNameAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
			left1.add(guestNameAF);
			
			titleLabel = new Label();
			titleLabel.setId("FolioDialog_titleLabel");
			
			titleAF = new AdapterField(titleLabel);
			titleAF.setId("FolioDialog_titleAF");
			titleAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.title());
			left1.add(titleAF);
			
			genderLabel = new Label();
			genderLabel.setId("FolioDialog_genderLabel");
			
			genderAF = new AdapterField(genderLabel);
			genderAF.setId("FolioDialog_genderAF");
			genderAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.gender());
			left1.add(genderAF);
			
			noOfGuestLabel = new Label();
			noOfGuestLabel.setId("FolioDialog_noOfGuestLabel");
			
			noOfGuestAF = new AdapterField(noOfGuestLabel);
			noOfGuestAF.setId("FolioDialog_noOfGuestAF");
			noOfGuestAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.noOfGuest());
			left1.add(noOfGuestAF);
			
			LayoutContainer right1 = new LayoutContainer();
			right1.setId("FolioDialog_right1");
			
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(160);
			layout.setDefaultWidth(160);
			right1.setLayout(layout);
			
			vipCodeLabel = new Label();
			vipCodeLabel.setId("FolioDialog_vipCodeLabel");
			
			vipCodeAF = new AdapterField(vipCodeLabel);
			vipCodeAF.setId("FolioDialog_vipCodeAF");
			vipCodeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.vipCodes());
			right1.add(vipCodeAF);
			
			roomNoLabel = new Label();
			roomNoLabel.setId("FolioDialog_roomNoLabel");
			
			roomNoAF = new AdapterField(roomNoLabel);
			roomNoAF.setId("FolioDialog_roomNoAF");
			roomNoAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.roomNo());
			right1.add(roomNoAF);
			
			arrivalDateAndTimeLabel = new Label();
			arrivalDateAndTimeLabel.setId("FolioDialog_arrivalDateAndTimeLabel");
			
			arrivalDateAndTimeAF = new AdapterField(arrivalDateAndTimeLabel);
			arrivalDateAndTimeAF.setId("FolioDialog_arrivalDateAndTimeAF");
			arrivalDateAndTimeAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.arrivalDateAndTime());
			right1.add(arrivalDateAndTimeAF);
			
			departureDateAndTimeLabel = new Label();
			departureDateAndTimeLabel.setId("FolioDialog_departureDateAndTimeLabel");
			
			departureDateAndTimeAF = new AdapterField(departureDateAndTimeLabel);
			departureDateAndTimeAF.setId("FolioDialog_departureDateAndTimeAF");
			departureDateAndTimeAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.departureDateAndTime());
			right1.add(departureDateAndTimeAF);
			
			payMethodLabel = new Label();
			payMethodLabel.setId("FolioDialog_payMethodLabel");
			
			payMethodAF = new AdapterField(payMethodLabel);
			payMethodAF.setId("FolioDialog_payMethodAF");
			payMethodAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod());
			right1.add(payMethodAF);
			
//			paymentMethodListStore = new ListStore<PaymentMethodModelData>();
//			paymentMethodCombo = new EasyClearCombobox<PaymentMethodModelData>();
//			paymentMethodCombo.setId("FolioDialog_paymentMethodCombo");
//			paymentMethodCombo.setStore(paymentMethodListStore);
//			paymentMethodCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod());
//			paymentMethodCombo.setDisplayField(CServiceConstants.PAY_MENT_METHOD);
//			paymentMethodCombo.setTriggerAction(TriggerAction.ALL);
//			right1.add(paymentMethodCombo);
			
			lc1.add(left1, new ColumnData(.45));
			lc1.add(right1, new ColumnData(.55));
			
			add(lc1);
			
			remarksLabel = new Label();
			remarksLabel.setId("FolioDialog_remarksLabel");
			
			remarksAF = new AdapterField(remarksLabel);
			remarksAF.setId("FolioDialog_remarksAF");
			remarksAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks());
			
			add(remarksAF);
			
			simGridPanel = new MySimpeGridPanel("",null);
			simGridPanel.setId("FolioDialog_simGridPanel_id");
			simGridPanel.setHeaderVisible(false);
			add(simGridPanel);
			
			Label label = new Label(CServiceConstants.CSERVICE_CONSTANTS.totalAmount() + ":");
			
			totalAmountLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.totalAmount());
			totalAmountLabel.setId("FolioDialog_totalAmountLabel");
			
			totalAmountAF = new AdapterField(totalAmountLabel);
			totalAmountAF.setId("FolioDialog_totalAmountAF");
			totalAmountAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.totalAmount());
			
			exportBtn = new FormButton(CoreConstants.CORE_CONSTANTS.export());
			exportBtn.setId("FolioDialog_exportBtn_id");
			exportBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "30px");
			exportBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					Window.alert("export");
					HashMap<String, String> params = new HashMap<String, String>();
					params.put(CServiceReportConstants.FOLIOID, Long.toString(currentFolio.getFolioId()));
					reportFrame.setUrl(HttpHelper
							.genHttpRequest("report/folio.do", params));
					Window.alert("passing");
				}
			});
//			getButtonBar().add(exportBtn);
			reportFrame = new Frame();
			reportFrame.setPixelSize(700, 700);
			reportFrame.setVisible(true);
			add(reportFrame);
			
			okButton = new FormButton(CoreConstants.CORE_CONSTANTS.ok());
			okButton.setId("FolioDialog_okButton_id");
			okButton.setWidth(60);
			okButton.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "360px");
			okButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					FolioDialog.this.hide();
				}
			});
			
			lc2 = new LayoutContainer();
			lc2.setId("FolioDialog_lc2");
			lc2.setLayout(new ColumnLayout());
			lc2.add(label, new ColumnData(100));
			lc2.add(totalAmountAF, new ColumnData(100));
			lc2.add(okButton, new ColumnData(70));
			lc2.add(exportBtn, new ColumnData(70));
			
			add(lc2);
		}

		@Override
		public void updateForm(final FolioView entity) {
			currentFolio = entity;
			Scheduler.get().scheduleFinally(new ScheduledCommand() {
				@Override
				public void execute() {
					exportBtn.setVisible(true);
//					if(entity.getGuestSignatureDateTime() != null){
//						exportBtn.setVisible(true);
//					}else{
//						exportBtn.setVisible(false);
//					}
				}
			});
			folioNoLabel.setText(entity.getFolioNo());	
			guestNameLabel.setText(entity.getGuestFirstName() + "," + entity.getGuestLastName());
			for(Title title : reservationConf.getTitleList()){
				if(title.getTitleId().equals(entity.getTitleId())){
					titleLabel.setText(title.getTitle());
				}
			}
			
			genderLabel.setText(entity.getGenderMale() ? 
					GenderGWTEnum.MALE.getDisplayStr() : GenderGWTEnum.FEMALE.getDisplayStr());
			noOfGuestLabel.setText(entity.getNumberOfGuest()+ "");
			vipCodeLabel.setText(entity.getVipCodes());
			roomNoLabel.setText(entity.getRoomNumber());
			arrivalDateAndTimeLabel.setText(CServiceConstants.DATE_FORMAT3.format(entity.getArrivalDateTime()));
			departureDateAndTimeLabel.setText(CServiceConstants.DATE_FORMAT3.format(entity.getDepartureDateTime()));
			for(PaymentMethod pm : reservationConf.getPaymentMethodList()){
				if(pm.getMethodId().equals(entity.getPaymethodId())){
					payMethodLabel.setText(pm.getMethod());
				}
			}
//			paymentMethodListStore.removeAll();
//			for(PaymentMethod pm : reservationConf.getPaymentMethodList()){
//				paymentMethodListStore.add(new PaymentMethodModelData(pm));
//			}
//			paymentMethodCombo.setValue(paymentMethodListStore.findModel(CoreConstants.ID, entity.getPaymethodId()));
			
			remarksLabel.setText(entity.getRemarks());
			
			double amount = 0;
			for(BillingView billing : entity.getBillings()){
				simGridPanel.addModel(new BillingModelData(billing));
				amount = amount + billing.getBillingAmount();
			}
			totalAmountLabel.setText(amount + "");
		}

		@Override
		public void populateEntity(FolioView entity) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
	}

	@Override
	public void hide() {
		super.hide();
		reportFrame.setUrl("");
		if (!isFirst) {
			formPanel.simGridPanel.removeAllModels();
		}
		isFirst = false;
	}
}
